package com.mfp.java.testYou.core.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

import com.mfp.java.testYou.core.AppContext;

public class Utils {

	private static final Logger log = Logger.getLogger(Utils.class);

	private Utils() {
	}

	/**
	 * Will print the contents of a String -- String[] map to a single String
	 * 
	 * @param map
	 *            map of type String,String[] (ex. request.getParameterMap())
	 * @return String
	 */
	public static String printMap(Map<String, String[]> map) {
		StringBuilder sb = new StringBuilder();
		for (Map.Entry<String, String[]> entry : map.entrySet()) {
			sb.append('[');
			sb.append(entry.getKey());
			sb.append('=');
			for (String elem : entry.getValue()) {
				sb.append(elem);
				sb.append(',');
			}
			sb.append(']');
		}
		return sb.toString();
	}

	/**
	 * 
	 * @param fileName
	 *            name of properties file
	 * @param key
	 *            of property
	 * @return value of property
	 */
	public static String readConfig(String fileName, String key) {
		String filePath = AppContext.getConfigPath() + File.separator;
		Properties props = new Properties();
		try {
			props.load(new FileInputStream(filePath + fileName));
			String val = props.getProperty(key);
			return val;
		} catch (IOException e) {
			log.error("Error reading config: " + e.getClass().getName() + " " + e.getMessage());
			return null;
		}
	}
}
